using System.Collections.Generic;
using System.Text.RegularExpressions;
/// <summary>
/// AssetBundle资源包相关扩展
/// </summary>
public static class HFUnityAssetBundleExtendEngine
{
    #region AssetBundleKey 获得资源路径Key
    /// <summary>
    /// 获得资源路径Key
    /// </summary>
    /// <param name="_aseetPath">资源路径</param>
    /// <returns>资源路径Key</returns>
    public static int HFAssetBundleKey(this string _aseetPath)
    {
        return _aseetPath.ToLower().HFTransPathSeparatorCharToUnityChar().HFUniqueHashCode();
    }
    #endregion

    #region AssetBundleEnum 获得资源路径枚举名称
    /// <summary>
    /// 获得资源路径枚举名称
    /// </summary>
    /// <param name="_aseetPath">资源路径</param>
    /// <returns>资源路径Key</returns>
    public static string HFAssetBundleEnum(this string _aseetPath)
    {
        return _aseetPath.HFAssetBundleKey().HFUniqueHex();
    }
    #endregion

    #region RemoveAssetBundleManifestNameHash 移除资源包ManifestName的Hash值
    /// <summary>
    /// 仅保留【字符、数字、下划线】
    /// </summary>
    readonly static string mcRegex_RemoveAssetBundleManifestNameHash = $@"(_[A-Za-z0-9]{{32}})(\{HFGlobalUtility.assetBundleNameSuffixExt.ext})";
    /// <summary>
    /// 移除资源包ManifestName的Hash值
    /// </summary>
    /// <param name="_assetBundleManifestName">资源包ManifestName</param>
    /// <returns>移除资源包ManifestName的Hash值的名称</returns>
    public static string HFRemoveAssetBundleManifestNameHash(this string _assetBundleManifestName)
    {
        return string.IsNullOrEmpty(_assetBundleManifestName) ? "" : Regex.Replace(_assetBundleManifestName, mcRegex_RemoveAssetBundleManifestNameHash, "$2");
    }
    #endregion

    #region RemoveAssetBundleExt 移除资源包名的指定后缀名
    /// <summary>
    /// 指定后缀枚举的值与后缀属性映射
    /// </summary>
    static Dictionary<int, HFFileExtAttribute> m_enHFUnityFileExt_Attr_Maping = typeof(enHFUnityFileExt).HFValueToAttribute<HFFileExtAttribute>();
    /// <summary>
    /// 移除资源包名的指定后缀名
    /// </summary>
    /// <param name="_assetBundleName">资源包名</param>
    /// <param name="_ext">后缀</param>
    public static string HFRemoveAssetBundleExt(this string _assetBundleName, enHFUnityFileExt _ext)
    {
        int key = (int)_ext;
        HFFileExtAttribute attr = null;
        if (m_enHFUnityFileExt_Attr_Maping.TryGetValue(key, out attr))
        {
            _assetBundleName = _assetBundleName.HFRemoveAssetBundleExt(attr);
        }
        return _assetBundleName;
    }

    /// <summary>
    /// 移除资源包名的指定后缀名
    /// </summary>
    /// <param name="_assetBundleName">资源包名</param>
    /// <param name="_ext">后缀</param>
    public static string HFRemoveAssetBundleExt(this string _assetBundleName, HFFileExtAttribute _extAttr)
    {
        return string.IsNullOrEmpty(_assetBundleName) ? _assetBundleName : Regex.Replace(_assetBundleName, $@"(\s+?|\S+?)({_extAttr.ext})+?", "$1");
    }
    #endregion

    #region ToUnityFileExt 后缀字符串转枚举    
    /// <summary>
    /// 后缀字符与枚举映射【带.号】
    /// </summary>
    static Dictionary<string, enHFUnityFileExt> m_enHFUnityFileExt_extMaping = typeof(enHFUnityFileExt).HFSpecialValueToEnum<string, HFFileExtAttribute, enHFUnityFileExt>((e, a) => { return a.ext; });
    /// <summary>
    /// 后缀字符与枚举映射【不带.号】
    /// </summary>
    static Dictionary<string, enHFUnityFileExt> m_enHFUnityFileExt_noDotExtMaping = typeof(enHFUnityFileExt).HFSpecialValueToEnum<string, HFFileExtAttribute, enHFUnityFileExt>((e, a) => { return a.noDotExt; });
    /// <summary>
    /// 后缀字符串转枚举【带.号】
    /// </summary>
    /// <param name="_ext">后缀[例如：.jpg|.prefab]</param>
    /// <returns>后缀枚举</returns>
    public static enHFUnityFileExt HFToUnityFileExt(this string _ext)
    {
        enHFUnityFileExt ext = enHFUnityFileExt.Unknown;
        m_enHFUnityFileExt_extMaping.TryGetValue(_ext, out ext);
        return ext;
    }

    /// <summary>
    /// 后缀字符串转枚举【不带.号】
    /// </summary>
    /// <param name="_ext">后缀[例如：jpg|prefab]</param>
    /// <returns>后缀枚举</returns>
    public static enHFUnityFileExt HFToUnityFileExtNoDot(this string _ext)
    {
        return m_enHFUnityFileExt_noDotExtMaping[_ext];
    }
    #endregion
}
